#include "Computer.h"
#include "Player.h"
#include "Card.h"
#include "Game.h"
#include "Human.h"
#include "Meld.h"
#include "Group.h"
#include "Run.h"
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

/*
#ifndef GAME_H
#define GAME_H

#endif
*/

void Game::populateDeck( void )
{
  for (int i =  1 ; i<= 13; i++)
  {
   for (int j = 0; j <= 3; j++)
      {
        Card newCard(i, Suit(j));
        deck.push_back(newCard);
        deck.push_back(newCard);
      }
  }
}



void Game::start( void ) {
  system("clear");

  this->populateDeck();
 
  cout<< "Greetings, earthlings. I am Zorlag The Magnificient, and I come in peace!"<< endl;
 
  askForPlayers(); //Done!
  
  system("clear");

  runGame(); 
 
}



void Game::runGame(void) {

  vector<Meld *>::iterator tableTerator;
  vector<Meld *>::iterator tableTwoTerator;
  vector<Card>::iterator handIteratorToDisplay;
  vector<Card>::iterator tableCardTerator;
  bool tableInvalid = 0;
  bool incremented = false;
  

  int i = 0;//-1 
  do{
    //  while (!players[i]->hasWon){    
    i = (i)%players.size();
    cout<<"Player "<<i;
    bool previousMoveDone = players[i]->firstMoveDone;
   
    if(!players[i]->move(table2))
      {     players[i]->hand.push_back(randPick(deck));
					i++;
					if ((players[i-1]->name).find("Zorlag's Minion")==-1)   system ("clear");
incremented = true; }
    else{
      tableTwoTerator = table2.begin();
      while (tableTwoTerator < table2.end()){
	if ( !((**tableTwoTerator).validify())) tableInvalid = 1; 
	if ( !(players[i]->firstMoveDone))      
	     tableInvalid = 1; 
	tableTwoTerator++;
	if ((players[i]->name).find("Zorlag's Minion")==-1)   system ("clear");
}

if(!tableInvalid)
{ 

 vector<Meld *>::iterator tableIterator = table.begin();
 while(tableIterator!=table.end()){
   delete *tableIterator; 
   table.erase(tableIterator);
 }

tableTwoTerator = table2.begin();
 while (tableTwoTerator < table2.end()){
   if ( (**tableTwoTerator).validify() == -1)  table.push_back( new Group( (**tableTwoTerator).getCardSet()));
   if ( (**tableTwoTerator).validify() == 1)  table.push_back(new Run( (**tableTwoTerator).getCardSet()) );
tableTwoTerator++;				        }  

 
//      if (!tableInvalid){
//	table = table2;

	players[i]->hand = players[i]->handBuffer;

	if(players[i]->handBuffer.size()==0){
	 players[i]->hasWon=true;
	 //
	  cout<<players[i]->name<<", you have won!"<<endl;
          cout<< "Puny Humans, Prepare To Die."<<endl;
 }
i++;
incremented = true;
}

  if (tableInvalid) {cout << "FRMGAME Invalid move, please try again.\n"; tableInvalid = 0;
    players[i]->firstMoveDone = previousMoveDone;
incremented = false;
 vector<Meld *>::iterator tableIterator2 = table2.begin();
 while(tableIterator2!=table2.end()){
   delete *tableIterator2; 
   table2.erase(tableIterator2);
 }

 
 vector<Meld *>::iterator tableTerator = table.begin();
 while(tableTerator!=table.end()){
   //   cout<<"THIS ITERATORPOINT: "<<**tableTerator<<endl;
   if ( (**tableTerator).validify() == -1)  table2.push_back( new Group( (**tableTerator).getCardSet()));
   if ( (**tableTerator).validify() == 1)  table2.push_back(new Run( (**tableTerator).getCardSet()) );
   tableTerator++;
 }
 
 }
 
     

    }
 

  }
  while((incremented && !players[i-1]->hasWon) || (!incremented));
  
  //  cout<<players[i]->name<<", you have won!"<<endl;
}


void Game::askForPlayers(void){
  
  string inString; string inName; int i =0 ;
  Player *tempPlayers[5];
  
  while ( inString.compare("end")) {
    cout<< "Enter type of player " <<i << "." << endl << "'H' or 'C' for (H)uman or (C)omputer.\n'end' exits player generation and begins the game." <<endl;
    cin>>inString;
    if (inString[0] == 'C' ||inString[0] == 'c' )
      { inName = "Zorlag's Minion ";      
	inName.push_back(i+65);
	players.push_back(new Computer (inName));
      }
    else if (inString[0] == 'H' || inString[0] == 'h')
      { 
	inName = "Human ";
	inName.push_back(i+65);
	cout << "Enter name of "<<inName<< ": ";
	inName = "";
	cin>>inName;
	players.push_back(new Human (inName));
      }
    else if (inString != "end")
      { cout << "Try again if you dare!"; }
    
    if (inString != "end"){
      for (int u = 0; u <13; u++)
	(players[i]->hand).push_back(randPick(this->deck));
    }
    
    // cout<<  "Number of Cards left in deck: " <<deck.size() << endl;    
    i++;
  }
  if (i-1>5 || i-1 <2) { cout << " You have not provided us with the right number of players. We will not play this game. Quitting."; exit(0); }
}



Card Game::randPick( vector<Card> & bunchOfStuffToPickFrom ) {
  if (bunchOfStuffToPickFrom.size() == 0) {cout << "Arrg! This Puny human game has run out of cards. It's a tie -----------------------------------------------------------------------------------------------\n";
    exit(1);}
  srand((unsigned)time(0));
  int randomNumberForCard;
  int lowest=0, highest=bunchOfStuffToPickFrom.size();
  randomNumberForCard = (rand()%highest);
  
  Card returnCard =  bunchOfStuffToPickFrom[randomNumberForCard];
  bunchOfStuffToPickFrom.erase (bunchOfStuffToPickFrom.begin() + randomNumberForCard );
  return returnCard;
  
  //cout << randomNumberForCard << " << RAND" << endl;  
}
